package com.micro.ai.files.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.micro.ai.commons.exception.BusinessException;
import com.micro.ai.files.entity.FileOperation;
import com.micro.ai.files.mapper.FileOperationMapper;
import com.micro.ai.files.service.FileOperationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 文件操作日志服务实现
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@Service
public class FileOperationServiceImpl extends ServiceImpl<FileOperationMapper, FileOperation> implements FileOperationService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FileOperation record(FileOperation operation) {
        if (operation == null) {
            throw new BusinessException("F0000", "操作日志信息不能为空");
        }
        
        if (!StringUtils.hasText(operation.getFileId())) {
            throw new BusinessException("F0000", "文件ID不能为空");
        }
        
        if (!StringUtils.hasText(operation.getOperationType())) {
            throw new BusinessException("F0000", "操作类型不能为空");
        }
        
        if (!StringUtils.hasText(operation.getOperatorId())) {
            throw new BusinessException("F0000", "操作人ID不能为空");
        }
        
        operation.setOperationTime(LocalDateTime.now());
        
        boolean success = this.save(operation);
        if (!success) {
            throw new BusinessException("F0003", "记录操作日志失败");
        }
        
        log.debug("记录操作日志成功: operationId={}, fileId={}, operationType={}", 
                operation.getId(), operation.getFileId(), operation.getOperationType());
        return operation;
    }

    @Override
    public Page<FileOperation> pageQuery(int pageNum, int pageSize, String tenantId, 
                                         String fileId, String operatorId, String operationType) {
        if (pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize < 1 || pageSize > 100) {
            pageSize = 20;
        }
        
        Page<FileOperation> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<FileOperation> query = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(tenantId)) {
            query.eq(FileOperation::getTenantId, tenantId);
        }
        if (StringUtils.hasText(fileId)) {
            query.eq(FileOperation::getFileId, fileId);
        }
        if (StringUtils.hasText(operatorId)) {
            query.eq(FileOperation::getOperatorId, operatorId);
        }
        if (StringUtils.hasText(operationType)) {
            query.eq(FileOperation::getOperationType, operationType);
        }
        
        query.orderByDesc(FileOperation::getOperationTime);
        
        return this.page(page, query);
    }

    @Override
    public List<FileOperation> listByFileId(String fileId) {
        LambdaQueryWrapper<FileOperation> query = new LambdaQueryWrapper<>();
        query.eq(FileOperation::getFileId, fileId)
             .orderByDesc(FileOperation::getOperationTime);
        
        return this.list(query);
    }

    @Override
    public List<FileOperation> listByOperator(String tenantId, String operatorId) {
        LambdaQueryWrapper<FileOperation> query = new LambdaQueryWrapper<>();
        query.eq(FileOperation::getTenantId, tenantId)
             .eq(FileOperation::getOperatorId, operatorId)
             .orderByDesc(FileOperation::getOperationTime);
        
        return this.list(query);
    }
}

